Repository navigation
Reimplement overload-and-remove palette-packing algorithm with undivided fractions to avoid integer overflow - #2250
Merged
Merged
Conversation
Contributor
Author
|
Confirmed that this works on Apple OSes older than what CI provides (macOS 11 with clang++ 22, and Mac OS X 10.8 with g++ 14, both available on MacPorts). |
Member
|
Could be nice to have a those “overly slow tests” still in the repo so they are less likely to get lost to time than in a PR or issue thread, even if they aren't tested (at least by default). Idk. |
ISSOtm
reviewed
Nov 25, 2026
ISSOtm
left a comment
Member
There was a problem hiding this comment.
I haven't looked at the meat of the changes yet, but here are already some comments.
ISSOtm
approved these changes
Nov 26, 2026
ISSOtm
left a comment
Member
There was a problem hiding this comment.
Awesome, thank you for thinking all of this through :) Just a few relatively minor things left, the core logic is a-OK to me!
ISSOtm
reviewed
Nov 26, 2026
ISSOtm
reviewed
Nov 26, 2026
This matches the design of the other three programs, and avoids silent overflow of `uint16_t` or `uint8_t` bounds.
…ded fractions to avoid integer overflow Assisted-by: Codex:GPT-6
Rangi42
force-pushed
the
pal-fractions
branch
from
November 27, 2026 20:00
8a7255f to
d25ce61
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2245, supersedes #2246.
This should be reviewed one commit at a time and rebase-merged, not squash-merged. The first two commits are preliminary: I wanted the
MAX_NB_PALETTESconstant for this PR, and noticed while adding it that our CLI arg handling was off, e.g. treating-d 258like a valid-d 2. The last commit is a refactor I'm not entirely sure is worthwhile.The justification for this PR is explained in the issue. Many thanks to @aaaaaa123456789 for feedback on that problem, confirming that the previous UB was expected and can be avoided; and to @vulcandth for the Astra PR that this one is based on.
I had the initial idea for
Fractions that Astra implemented, and in this PR, I independently rederived the logic about maximum limits which prove that overflow can't occur. (I actually only skimmed Astra's explanation, so after calculating the numeric values of myMAX_PAL_COLORS/MAX_NB_PALETTES/MAX_NB_COLOR_SETSformulas and seeing the exact same numbers in Astra's analysis, it raises my confidence that both are correct.)I did base the
Fraction::compareimplementation on Astra's, after re-deriving and commenting it to trust that it's correct. Two test cases are also by Astra -- they're important because they would fail if I'd just used the "minimal fix" shortcuts suggested by my initial analysis with DeepSeek 4.1 Flash. (I manually-reversed all the test cases to make sure the.output is correct.)Argubaly I overdid it with the comments and assertions, but this is the third reimplementation of the relative-size/efficiency calculation (after we discovered issues with floating-point and then with LCM), so I want to err on the side of being clear and certain. @ISSOtm Feel free to reword or trim them though!
In addition to the committed test cases, I tried the maximal 768-tile
foo.png(which just barely fits into 256 palettes):With a regular optimized build,
time ./rgbgfx -n 256 -o foo.2bpp -OPQT foo.pngtakes justreal 0m1.736son my machine. However, with adevelopbuild, it takesreal 2m53.344s, which would be prohibitively slow for our CI.This relatively slow performance is expected; from the original paper:
You can see how that O(768^3) pans out with
-vvvvvverbose logging. It's a very unrealistic case though, so not something to worry about. :P